package businessEntities;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import DAO.DaoActif;
import DAO.DaoCours;


public class DateCours
{
	public static final String FORMAT_YYYY_MM_DD= "yyyy/MM/dd";
	public static final String FORMAT_DD_MM_YYYY= "dd/MM/yyyy";
	public static final String FORMAT_DD_MM_YYYY_TIRE= "dd-MM-yyyy";
	public static final String FORMAT_DD_MM_Y= "dd/MM/yy";
	public static final String FORMAT_YYYYY_MM_DD_TIRE= "yyyy-MM-dd";
	
	public static Date getDate(String sDate,String format){
		Date d;
		DateFormat dateFormat ;
		dateFormat = new SimpleDateFormat(format);
		
		try {
			d = dateFormat.parse(sDate);
		} catch (Exception e) {
			d = null;
		}
		
		//System.out.println("getDate: "+d+" sDate: "+sDate+" format: "+format);
		return d;
	}
	
	public static String getDate(Date date, String format){
		DateFormat dateFormat ;
		dateFormat = new SimpleDateFormat(format);
		return dateFormat.format(date);
	}
	
	public static Date getNextDate(Date t, int delta) {
		
		Calendar c = new GregorianCalendar();
		int nbDay = 0;


		do{
			c.setTime(t);
			c.add(c.DAY_OF_YEAR, delta);
			c.add(c.DAY_OF_YEAR, nbDay);
			nbDay++;
			
		}while(c.get(c.DAY_OF_WEEK)==c.SUNDAY || c.get(c.DAY_OF_WEEK)==c.SATURDAY);
		
		return c.getTime();
	}
	
	public static int getPeriodeJours(Date dateDebut, Date dateFin) {
		if(dateDebut==null || dateFin==null)return 0;

		Calendar calendar1 = new GregorianCalendar();
		Calendar calendar2 = new GregorianCalendar();
		Calendar calendar3 = new GregorianCalendar();
		
		calendar1.setTime(dateDebut);
		calendar2.setTime(dateFin);
	
		calendar3.setTimeInMillis(calendar2.getTimeInMillis()-calendar1.getTimeInMillis());
		return (int)((calendar2.getTimeInMillis()-calendar1.getTimeInMillis())/(1000*60*60*24));
	}
	
	public static Date getDate(Date t, int year, int month, int day) {
		
		Calendar c = new GregorianCalendar();
		c.setTime(t);
		c.add(c.YEAR, year);
		c.add(c.MONTH, month);
		c.add(c.DAY_OF_YEAR, day);
		return c.getTime();
	}
	
	public static void main(String[] args) {
		//Actif a = DaoActif.getActif("QS0010040911");
		Date deb = DateCours.getDate("02/06/2010",DateCours.FORMAT_DD_MM_YYYY);
		Date fin = DateCours.getDate("03/06/2010",DateCours.FORMAT_DD_MM_YYYY);
		System.out.println(getPeriodeJours(deb, fin));
	}
}

